package com.lancoo.klgcourseware.ui.fragment.pronounceTrain;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.base.BaseKlgFragment;
import com.lancoo.klgcourseware.entity.WordArticulation;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.BasicTrainBaseFragment;
import com.lancoo.klgcourseware.utils.KlgTrainUtils;
import com.stkouyu.LancooSkEgnManager;
import com.stkouyu.entity.LgEvaluateObj;

import java.util.List;

/**
 * Created by pengmi on 2019/8/21 20:39
 * Description:发音训练子模式基类
 */
public abstract class PronounceBaseFragment extends BaseKlgFragment {


    protected WordArticulation wordArticulation;

    public enum PronounceState {
        SLOWDREAD, FASTREAD,SLOWREPEAT,FASTREPEAT
    }
    protected PronounceState pronounceState = PronounceState.SLOWDREAD;
    protected ImageView img_pause;


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        wordArticulation = (WordArticulation) getArguments().getSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION);
        super.onViewCreated(view, savedInstanceState);
    }

    protected void startRecordSpeechEvaluation(String url,String content){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).startRecordSpeechEvaluation(url,content);
        }
    }

    protected void stopRecordSpeechEvaluation(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).pauseTimer();
            ((BasicTrainBaseFragment)getParentFragment()).stopRecordSpeechEvaluation();
        }
    }

    protected void scrollIndex(RecyclerView recyclerView, int position, int totalSize, boolean forward){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).autoScroll(recyclerView,position,totalSize,forward);
        }
    }

    protected void startDripMusic(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).startDripMusic();
        }
    }

    protected void enterPronounceResultActivity(int score, List<String> recordUrlList,String resultModel){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).enterPronounceResultActivity(score,recordUrlList,resultModel);
        }
    }

    protected void enterPronunceRepeatNextTrain(int score, String recordUrl){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).enterPronounceRepeatNextTrainModel(score,recordUrl);
        }
    }

    protected void startSecondTimer(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).startSecondCountTime();
        }
    }

    /**
     * 我不练了
     */
    protected void giveUpTrain(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).giveUpCurrentTrain();
        }
    }

    protected void upDateTrainResult(boolean isQuality){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
//            ((BasicTrainBaseFragment)getParentFragment()).updateCurrentTrainResult(isQuality);
        }
    }
    protected void upDateTrainResult(boolean isQuality,int grade){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).updateCurrentTrainResult(isQuality,grade);
        }
    }

    protected float getSlowRepeatScore(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            return ((BasicTrainBaseFragment)getParentFragment()).getSlowRepeatScore();
        }else {
            return 0;
        }

    }

    protected void playResultMusic(boolean isCorrect){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).usageQuesTrainResultMediaPlayer(isCorrect);
        }
    }


    protected void upDateRecordAnswerProgress(int recordProgress){ }

    protected void startSubModelTrain(){}

    protected void onSuccessSpeechEvaluation(int grade, String url, LgEvaluateObj skegnResult){}

    protected void pauseTrain(boolean isReset){}

    protected void upDateRepeatProgress(int time){}

    protected void continueTrain(){}

    protected int getStratTrainProgress(){
        return 0;
    }

    protected void updateCountTime(int progress,int totalProgress){}

    protected void updateResultCountTime(int progress,int totalProgress){}



    /************外部调用方法***************/
    public void upDateRecordProgress(int progress){
        upDateRecordAnswerProgress(progress);
        audioAnimation();
    }

    public void startTrain(){
        startSubModelTrain();
    }

    public void speechEvaluationSuccess(int grade, String url, LgEvaluateObj skegnResult){
        onSuccessSpeechEvaluation(grade,url,skegnResult);
    }

    public boolean enableResetProgress(){
        return pronounceState!=PronounceState.FASTREAD;
    }

    /**
     * 获取快速朗读开始时间
     * @return
     */
    public int getFastReadingStartProgress(){
        return getStratTrainProgress();
    }

    /**
     * 中止当前的训练
     */
    public void pauseCurrentPronounceSubTrain(boolean isReset){
        pauseTrain(isReset);
    }

    public void upDateRepeatCountTime(int countTime){
        upDateRepeatProgress(countTime);
    }

    /**
     *恢复发音训练流程
     */
    public void resumePronounceTrainStudy(){
        continueTrain();
    }

    /**
     * 更新3，2，1倒计时进度
     * @param progress
     */
    public void updateCountTimeProgress(int progress,int totalProgress){
        updateCountTime(progress,totalProgress);
    }

    /**
     *作答评阅结束2s倒计时
     * @param progress
     */
    public void upDateResultCountTime(int progress,int totalProgress){
        updateResultCountTime(progress,totalProgress);
    }


    protected void volumeAnimationControl(ImageView img, boolean isAnimation){
        if(img!=null&&img.getVisibility()==View.VISIBLE){
            if(isAnimation){
//                img.setImageResource(R.drawable.lgklg_animation_list_volume_cyan);
                img.setBackground(null);
                ((AnimationDrawable)img.getDrawable()).start();
            }else{
//                img.setImageResource(R.drawable.lgklg_volume_cyan_3);
                img.setBackgroundResource(R.drawable.klg_volume_cyan_3);
                ((AnimationDrawable)img.getDrawable()).stop();
                ((AnimationDrawable)img.getDrawable()).selectDrawable(0);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(System.currentTimeMillis()-time<500){
            return;
        }
        time=System.currentTimeMillis();
        int id=v.getId();
        if(id==R.id.tv_pass_train){
            giveUpTrain();
        }
    }

    private void audioAnimation(){
        float volume = LancooSkEgnManager.getInstance(getContext()).getVolume();
        try {
            ImageView imgRecorder = convertView.findViewById(R.id.img_recorder);
            if(imgRecorder==null){
                return;
            }
            LayerDrawable drawable =(LayerDrawable) imgRecorder.getDrawable();
            Drawable clipDrawable = drawable.getDrawable(1);

            clipDrawable.setLevel(KlgTrainUtils.getAudioDBLevel((int)volume));
        }catch (Exception e){
            e.printStackTrace();
        }

    }


}
